package com.hilaver.dzmis.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Blob;
import java.util.List;

import org.hibernate.Hibernate;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiPhotoGallery;
import com.hilaver.dzmis.codetable.CtCollarType;
import com.hilaver.dzmis.codetable.CtMaterialType;
import com.hilaver.dzmis.codetable.CtPhotoType;
import com.hilaver.dzmis.codetable.CtProductFamily;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiPhotoGalleryServiceImpl extends AbstractBaseService {
	
	public final static String COLUMN_COLLAR_TYPE = "ctCollarType";
	
	public final static String COLUMN_PHOTO_TYPE = "ctPhotoType";

	public final static String COLUMN_BRAND = "ctBrand";
	
	private CtServiceImpl ctService;
	
	public BiPhotoGalleryServiceImpl(){
		this.ctService = new CtServiceImpl();
	}
	
	@Override
	public String delete(int id) throws Exception {
		return super.delete(BiPhotoGallery.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		BiPhotoGallery biPG = (BiPhotoGallery) this.dao.get(
				BiPhotoGallery.class.getName(), id);

		writeFile(realFileDir + biPG.getPhotoName(), biPG.getPhoto());

		StringBuffer xmlSB = new StringBuffer();

		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(biPG));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String edit(BiPhotoGallery biPG) throws Exception {
		if (biPG.getId() != null && biPG.getPhotoName() == null) {
			BiPhotoGallery old = (BiPhotoGallery) this.dao.get(
					BiPhotoGallery.class.getName(), biPG.getId());
			biPG.setPhoto(old.getPhoto());
			biPG.setPhotoName(old.getPhotoName());
			this.dao.getSession().evict(old);
		}
		this.dao.saveOrUpdate(biPG);
		return "{success: true}";
	}
	
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort, String order, int limit,
			String[] filters) throws Exception {
		int offset = limit * (page - 1);
		List<BiPhotoGallery> objList = this.dao.getAllPagination(BiPhotoGallery.class
				.getName(), offset, limit, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (BiPhotoGallery obj : objList) {
			
			writeFile(realFileDir + obj.getPhotoName(), obj.getPhoto());
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append("<" + COLUMN_PHOTO_TYPE + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtPhotoType.class.getName(), obj.getCtPhotoType(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_PHOTO_TYPE + ">");
			
			xmlSB.append("<" + COLUMN_MATERIAL_TYPE + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtMaterialType.class.getName(), obj.getCtMaterialType(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_MATERIAL_TYPE + ">");
			
			xmlSB.append("<" + COLUMN_COLLAR_TYPE + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtCollarType.class.getName(), obj.getCtCollarType(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_COLLAR_TYPE + ">");
			
			xmlSB.append("<" + COLUMN_PRODUCT_FAMILY + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtProductFamily.class.getName(), obj.getCtProductFamily(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_PRODUCT_FAMILY + ">");
			
			if(obj.getBiFactory() != null){
				xmlSB.append("<factoryNameCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getNameCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameCN>");
				
				xmlSB.append("<factoryNameEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getNameEN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameEN>");
				
				xmlSB.append("<addressFactoryCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getAddressFactoryCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</addressFactoryCN>");
				
				xmlSB.append("<addressFactoryEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getAddressFactoryEN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</addressFactoryEN>");
			}
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiPhotoGallery.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		String photoDirStr = "E:\\99 daafee\\06 Design Zone\\01 doc\\图片\\all";
		File photoDir = new File(photoDirStr);
		String[] fileNameArray = photoDir.list();
		BiPhotoGalleryServiceImpl service = new BiPhotoGalleryServiceImpl();
		for (int i = 0; i < fileNameArray.length; i++) {
			FileInputStream   fileInputStream   =   new   FileInputStream(photoDirStr + "\\" + fileNameArray[i]);   
			Blob photoBlob = Hibernate.createBlob(fileInputStream);
			String photoName = System.currentTimeMillis() + StringUtils.getFileExtension(fileNameArray[i]);
			BiPhotoGallery gallery = new BiPhotoGallery();
			
			gallery.setPhoto(photoBlob);
			gallery.setPhotoName(photoName);
			System.out.println(service.edit(gallery));
		}
	}

}
